package zhieasy.cn.service.forum.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.model.forum.ForumCommentRely;
import zhieasy.cn.model.forum.ForumCommentRoot;
import zhieasy.cn.persistence.mapper.forum.ForumCommentRelyMapper;
import zhieasy.cn.persistence.mapper.forum.ForumCommentRootMapper;
import zhieasy.cn.service.forum.ForumCommentRootService;

import java.util.HashMap;
import java.util.List;

/**
 * @author DaHang
 * @date 2020/8/29 16:38
 */
@Service
public class ForumCommentRootServiceImpl extends ServiceImpl<ForumCommentRootMapper, ForumCommentRoot> implements ForumCommentRootService {
    @Autowired
    ForumCommentRootMapper forumCommentRootMapper;
    @Autowired
    ForumCommentRelyMapper forumCommentRelyMapper;
    @Override


    public List<ForumCommentRoot> getCommonRootByForumId(int forumId, int pageIndex, int pageSize) {
        QueryWrapper<ForumCommentRoot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id",forumId);
        Page<ForumCommentRoot> myPage = new Page<>(pageIndex,pageSize);
        IPage<ForumCommentRoot> page = forumCommentRootMapper.selectPage(myPage, queryWrapper);
        List<ForumCommentRoot> rootCommons = page.getRecords();
        //拿到数据了，那就要把这些数据里面的所有子评论拿出来
        rootCommons.stream().forEach(common -> {
            //获取一下id
            int id = common.getId();
            //然后通过这个id把关于他的评论都找出来
            List<ForumCommentRely> forumCommentRelyList = forumCommentRelyMapper.selectByMap(new HashMap<String, Object>() {{
                put("owner_id", id);
            }});
            forumCommentRelyList.stream().forEach(forumCommonRely -> {
                if (forumCommonRely.getValid() == 0){
                    forumCommonRely.setContent("该内容已被折叠~");
                }
            });
            common.setForumCommentRelies(forumCommentRelyList);
        });
        return rootCommons;
    }


}
